package com.yunji.order.service;

import com.google.common.collect.Lists;
import com.yunji.common.utils.ConvertDataUtil;
import com.yunji.order.bo.GetAuditApplyBO;
import com.yunji.order.dao.IRefundDao;
import com.yunji.order.dao.IRefundDetailDao;
import com.yunji.order.domain.RefundDO;
import com.yunji.order.domain.RefundDetailDO;
import com.yunji.order.dto.OrderDetailRefundDTO;
import com.yunji.order.dto.RefundDetailParam;
import com.yunji.order.dto.RefundParam;
import com.yunji.order.emuns.RefundStatusEnum;
import com.yunji.order.vo.RefundDetailVO;
import com.yunji.order.vo.RefundVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Package com.yunji.order.service.impl
 * FileName:	RefundServiceImpl.java
 * Copyright:	Copyright © 2024 云集互联 All Rights Reserved
 * Company:		云集互联
 * Create at:   2024-04-21 21:25:05
 * Description:
 * 订单退款接口实现
 * Modification History:
 * Author					Date					Version         Description
 * -------------------------------------------------------------------------------------
 * HuaAo0803@163.com	2024-04-21 21:25:05     1.0             1.0 Version
 */
@Service
public class RefundService {
    @Resource
    private IRefundDao refundDao;

    @Resource
    private IRefundDetailDao refundDetailDao;

    // 申请中 退款中 已退款这三个状态代表 所有正在退款的数量
    public List<OrderDetailRefundDTO> getRefundDetailNumsByOid(List<Long> orderIds) {
        return refundDetailDao.getDetailNumsByOid(orderIds, Lists.newArrayList(RefundStatusEnum.APPLYING.getCode(),
                RefundStatusEnum.REFUNDING.getCode(), RefundStatusEnum.REFUNDED.getCode()));
    }

    // 获取明细各个状态的数量
    public List<OrderDetailRefundDTO> getStatusNumsByOid(List<Long> orderIds) {
        return refundDetailDao.getStatusNumsByOid(orderIds);
    }

    public Map<Long, List<RefundDetailDO>> getDetailMapByRids(List<Long> refundIds) {
        return ConvertDataUtil.getGroupBy(() -> refundDetailDao.getDetailsByRids(refundIds), RefundDetailDO::getRefundId);
    }

    public Map<Long, List<RefundDetailVO>> getDetailVOMapByRids(List<Long> refundIds) {
        return ConvertDataUtil.getGroupBy(() ->
                refundDetailDao.getDetailList(RefundDetailParam.builder().refundIds(refundIds).build()), RefundDetailVO::getRefundId);
    }

    public Map<Long, OrderDetailRefundDTO> getRefundDetailNumsMap(List<Long> orderIds) {
        return ConvertDataUtil.getDataMap(() -> getRefundDetailNumsByOid(orderIds), OrderDetailRefundDTO::getOrderDetailId);
    }

    public boolean createRefund(RefundDO refund) {
        return refundDao.save(refund);
    }

    public RefundDO getById(Long refundId) {
        return refundDao.getById(refundId);
    }

    public boolean createRefundDetail(List<RefundDetailDO> refundDetailDOS) {
        return refundDetailDao.saveBatch(refundDetailDOS);
    }

    public boolean updateRefund(RefundDO refund) {
        return refundDao.updateById(refund);
    }

    public RefundDO getByCode(Long refundId) {
        return refundDao.getById(refundId);
    }

    public RefundDO getByCode(String refundCode) {
        return refundDao.getByCode(refundCode);
    }


    public List<RefundVO> refundList(RefundParam param) {
        return refundDao.refundList(param);
    }

    public List<RefundDO> searchAuditRefund(GetAuditApplyBO bo) {
        return refundDao.listAuditRefund(bo);
    }

    public List<RefundDetailVO> refundDetailList(RefundDetailParam build) {
        return refundDetailDao.getDetailList(build);
    }

    public List<RefundDO> listByOid(Long orderId) {
        return refundDao.listByOrderId(orderId);
    }
}